import * as THREE from 'three'
import { EventBus } from '@/utils/EventBus'

export class Sky {
    constructor (scene) {
        this.scene = scene
        this.nowMesh = []
        this.nowSkyName = '展厅'
        this.createIndoorBg()
        this.init()
    }
    init() {
        EventBus.getInstance().on('changeSky', (skyName) => {
            if (skyName === this.nowSkyName) return
            this.clear()
            if (skyName === '展厅') {
                // 室内背景
                this.nowSkyName = '展厅'
                this.createIndoorBg()
            } else {
                // 室外
                this.nowSkyName = '户外'
                this.createOutDoorBg()
            }
        })
    }
    // 创建室内场景
    createIndoorBg () {
        // 球体
        const sphereGeo = new THREE.SphereGeometry(16, 32, 32)
        // 球体材质
        const sphereMaterial = new THREE.MeshBasicMaterial({
            color: 0x42454c,
            side: THREE.DoubleSide
        })
        // 球体物体
        const sphere = new THREE.Mesh(sphereGeo, sphereMaterial)
        this.scene.add(sphere)
        this.nowMesh.push(sphere)

        // 地面，
        const planeGeo = new THREE.CircleGeometry(10, 32)
        // 地面材质，因为是非基础，会反光，所以颜色会不一样
        const planeMaterial = new THREE.MeshStandardMaterial({
            color: 0x42454c, 
            side: THREE.DoubleSide
        })
        // 地面物体
        const plane = new THREE.Mesh(planeGeo, planeMaterial)
        // gui调整一下就知道为啥
        plane.rotation.set(-Math.PI / 2, 0, 0)
        plane.receiveShadow = true
        this.scene.add(plane)
        this.nowMesh.push(plane)
    }
    // 创建室外场景
    createOutDoorBg () {
        // 球体
        const sphereGeo = new THREE.SphereGeometry(40, 32, 16)
        // 材质
        const desertTexture = (new THREE.TextureLoader()).load('image/desert.jpg')
        desertTexture.colorSpace = THREE.SRGBColorSpace
        // 球体材质
        const sphereMaterial = new THREE.MeshBasicMaterial({
            map: desertTexture,
            side: THREE.DoubleSide
        })
        // 球体物体
        const sphere = new THREE.Mesh(sphereGeo, sphereMaterial)
        this.scene.add(sphere)
        this.nowMesh.push(sphere)

        // 地面，
        const planeGeo = new THREE.CircleGeometry(20, 32)
        // 材质
        const sandTexture = (new THREE.TextureLoader()).load('image/sand.jpg')
        sandTexture.colorSpace = THREE.SRGBColorSpace
        // 地面材质，因为是非基础，会反光，所以颜色会不一样
        const planeMaterial = new THREE.MeshStandardMaterial({
            map: sandTexture,
            side: THREE.DoubleSide
        })
        // 地面物体
        const plane = new THREE.Mesh(planeGeo, planeMaterial)
        // gui调整一下就知道为啥
        plane.rotation.set(-Math.PI / 2, 0, 0)
        plane.receiveShadow = true
        this.scene.add(plane)
        this.nowMesh.push(plane)
    }

    clear () {
        this.nowMesh.forEach(obj => {
            obj.geometry.dispose()
            obj.material.dispose()
            obj.material.map && obj.material.map.dispose()
            obj.parent.remove(obj)
        })
        this.nowMesh.splice(0, this.nowMesh.length)
    }

}